<?php

namespace app\admin\controller\metier\share;

use app\BaseController;
use app\model\MetierShareModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    protected MetierShareModel $mode;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->mode = new MetierShareModel();
    }

    /**
     * 上传人选择器
     * @return bool|string
     */
    public function userSelector(): bool|string
    {
        try {
            $uid = $this->mode->where('unit_id','=',$this->userData['unit_id'])->distinct(true)->column('uid');
            $UserModel = new UserModel();
            $data = $UserModel->field('id,name')->where('id','in',$uid)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 列表
     * @return bool|string
     */
    public function list(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require'
            ]);
            $where = [];
            if (!empty($param['title'])){
                $where[] = ['title','like','%'.$param['title'].'%'];
            }
            if (isset($param['status']) && strlen($param['status']) > 0){
                $where[] = ['status','=',$param['status']];
            }
            if (!empty($param['uid'])){
                $where[] = ['uid','=',$param['uid']];
            }
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $query = $this->mode->where($where);
            $data = $query->with([
                'user'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 详情
     * @return bool|string
     */
    public function info(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $info = $this->mode->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 添加
     * @return bool|string
     */
    public function add(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'title|职业' => 'require',
                'content|内容' => 'require',
                'cover|封面' => 'require',
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $this->mode->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 更新
     * @return bool|string
     */
    public function update(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
//                'title|职业' => 'require',
//                'content|内容' => 'require',
//                'cover|封面' => 'require',
            ]);
            $info = $this->mode->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 删除
     * @return bool|string
     */
    public function delete(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $this->mode->where('id','in',$param['id'])->update([
                'is_del' => 1,
                'del_time' => time()
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}